﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Model.WXshopBuiness.DTO;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;


namespace Repository.WXshopBuiness
{
   public static class AddressRep
    {

        public static Address GetById(this IRepositoryAsync<Address> repository, string Id)
        {
            Expression<Func<Address, bool>> expression = i => 1 == 1;
            if (string.IsNullOrEmpty(Id))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.AddressID == Id);
            }
            return repository.Query().Where(expression).Select().OrderByDescending(i => i.IsDefault).ThenByDescending(i => i.CreateTime).FirstOrDefault();
        }

        public static List<Address> GetMyAddress(this IRepositoryAsync<Address> repository, string userId)
        {
            Expression<Func<Address, bool>> expression = i => 1== 1;
            if (!string.IsNullOrEmpty(userId))
            {
                expression = expression.AndAlso(i => i.UserId == userId);
            }
            else
            {
                return null;
            }
             return repository.Query().Where(expression).Select().OrderByDescending(i=>i.IsDefault).ThenByDescending(i=>i.CreateTime).ToList();
        }
    }
}
